import 'package:flame/components.dart';
import 'package:flame/src/parallax.dart';
import 'package:flutter_flappy_bird/bird.dart';

import 'flappy_bird.dart';

final imageData = [
  ParallaxImageData('land.png'),
];

class LandComponent extends ParallaxComponent<FlappyBirdGame> {
  LandComponent(this.screenSize, this.birdComponent)
      : super(size: Vector2(screenSize.x, 96));

  Vector2 screenSize;
  final BirdComponent birdComponent;

  @override
  Future<void>? onLoad() async {
    parallax = await gameRef.loadParallax(
      imageData,
      baseVelocity: Vector2(60, 0),
    );
    return super.onLoad();
  }

  @override
  void update(double dt) {
    super.update(dt);
    if (birdComponent.getCurrentStatus() == BirdComponent.statusGameOver) {
      parallax?.baseVelocity = Vector2(0, 0);
    } else {
      parallax?.baseVelocity = Vector2(60, 0);
    }
  }

  @override
  void onGameResize(Vector2 size) {
    super.onGameResize(size);
    screenSize = size;
    this.size.x = screenSize.x;
    parallax?.size = this.size;
    position = Vector2(0, screenSize.y - 96);
  }
}
